﻿using Library.Core.Domain;
using Library.Core.Domain.Specification;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Aggregation.ValueObject.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Domain.Specifications.Authority.Menu
{
    public class CreateMenuSpecification : IOperateSpecification<MenuInfoEntity>
    {
        private readonly IMenuRepository _menuRepository;

        public CreateMenuSpecification(IMenuRepository menuRepository)
        {
            _menuRepository = menuRepository;
        }

        public async Task<bool> SatisfiedBy(MenuInfoEntity entity)
        {
            if (await _menuRepository.AnyAsync(x => x.Title.Equals(entity.Title) && x.ParentId.Equals(entity.ParentId)))
            {
                throw new DomainException("同一层级下,菜单名称重复");
            }
            return true;
        }
    }
}
